package com.qingxin.sys.entity;

import javax.servlet.http.HttpServletRequest;

import com.qingxin.sys.utils.StringUtils;

public class MyUploadFileParam {

	
	//类型
	private String type = "myupload";
	//修改现有文件记录
	private String[] mFileId;
	private String[] mFileName;
	private String[] mOrigName;
	private String[] mFileDesc;
	//新上传
	private String[] fileName;
	private String[] origName;
	private String[] fileDesc;
	//删除现有文件
	private String[] delId;
	
	
	
	public MyUploadFileParam(HttpServletRequest request) {
		this(null, request);
	}
	
	/**
	 * 构造指定类型上传参数
	 * @param type
	 * @param request
	 */
	public MyUploadFileParam(String type, HttpServletRequest request) {
		if(StringUtils.isNotEmpty(type)){
			this.type = type;
		}
		if(request.getParameterValues(this.type+"_mFileId") != null){
			this.mFileId = request.getParameterValues(this.type+"_mFileId");
			this.mFileName = request.getParameterValues(this.type+"_mFileName");
			this.mOrigName = request.getParameterValues(this.type+"_mOrigName");
			this.mFileDesc = request.getParameterValues(this.type+"_mFileDesc");
		}
		if(request.getParameterValues(this.type+"_fileName") != null){
			this.fileName = request.getParameterValues(this.type+"_fileName");
			this.origName = request.getParameterValues(this.type+"_origName");
			this.fileDesc = request.getParameterValues(this.type+"_fileDesc");
		}
		if(request.getParameterValues(this.type+"_delId") != null){
			this.delId = request.getParameterValues(this.type+"_delId");
		}
	}
	
	
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
	
	/**
	 * 是否有效
	 * @return
	 */
	public boolean isValid(){
		try {
			if(mFileId != null && (mFileId.length != mFileName.length
					|| mFileId.length != mOrigName.length
					|| mFileId.length != mFileDesc.length)){
				return false;
			}
			if(fileName != null && (fileName.length != origName.length
					|| fileName.length != fileDesc.length)){
				return false;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	
	/**
	 * 修改数量
	 * @return
	 */
	public int getModifyNum(){
		if(!isValid() || mFileId==null){
			return 0;
		}
		return mFileId.length;
	}
	
	public MyUploadFile getModify(int index){
		return new MyUploadFile(mFileId[index], mFileName[index], mOrigName[index], mFileDesc[index], null);
	}
	
	/**
	 * 新上传数量
	 * @return
	 */
	public int getUploadNum(){
		if(!isValid() || fileName==null){
			return 0;
		}
		return fileName.length;
	}
	
	public MyUploadFile getUpload(int index){
		return new MyUploadFile(null, fileName[index], origName[index], fileDesc[index], null);
	}
	
	/**
	 * 删除数量
	 * @return
	 */
	public int getDeleteNum(){
		if(!isValid() || delId==null){
			return 0;
		}
		return delId.length;
	}
	
	public String[] getDeleteIds() {
		return delId;
	}
	
	
}
